﻿#include<iostream>
#include<string>
using namespace std;

void test_string1()
{
	string s1;
	string s2("Hello world!");

	cin >> s1;
	cout << s1 << endl;

	string ret = s1 + s1;
	cout << ret << endl;
}

void test_string2()
{
	string s1("Hello world!");
	string s2 = "Hello world!";

	//遍历string
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}

	cout << endl;

	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}

	cout << s1 << endl;

	string::iterator it = s1.begin();
	while (it != s1.end()) //不推荐使用 `it < s1.end()`
	{
		cout << *it << " ";
		//*it = 'a';
		++it;
	}
	cout << s1 << endl;
}

void test_string3()
{
	string s1("buider");
	//string::reverse_iterator rit = s1.rbegin(); //rbegin -> 'r'  rend -> 'b'
	auto rit = s1.rbegin(); //改良上一行， auto用来自动推导变量的类型
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;  //++倒着走（反向迭代器）
	}

	cout << endl;

	//原理：编译时编译器替换为迭代器（看反汇编）
	for (auto ch : s1)//范围for（把*it赋值给ch, auto& ch就可以修改了），但是不支持倒着遍历
	{
		cout << ch << " ";
	}
	cout << endl;
	//string s1("buider");
	//string::iterator it = s1.end(); 
	//while (it != s1.begin())
	//{
	//	--it; --it;
	//	cout << *it << " ";
	//}
}

void func(const string& ts)
{
	auto it = ts.cbegin();
	while (it != ts.cend())
	{
		cout << *it << " ";
		++it;
	}
}

void test_string4()
{
	string s1("I'm coming!");

	func(s1);
}

void test_string5()
{
	string s1("I'm coming!123");
	string s2(s1, 4/*, 7*/); //第三个值为缺省值，不给的话就是取到结尾
	cout << s2 << endl;

	string s3(++s1.begin(), --s1.end());
	cout << s3 << endl;

	s2 = s3;

	cout << s2 << endl;
}

void test_string6()
{
	string s;
	size_t old = s.capacity();

	s.reserve(100); 

	for (size_t i = 0; i < 100; i++)
	{
		s.push_back('a');
		if (s.capacity() != old)
		{
			cout << "expand:" << s.capacity() << endl;
			old = s.capacity();
		}
	}
}

void test_string7()
{
	string str = "hello world";
	cout << str.size() << endl;
	cout << str.capacity() << endl;

	str.resize(15/*, 'x'*/);
	cout << str << endl;


	str.push_back('!');
	str.append("Bye!");

	cout << str << endl;
}

void test_string8()
{
	string str("Nihao");

	str.insert(0, 1, 'x');
	str.insert(str.begin(), 3, 'y');

	cout << str << endl;

	str.erase(0, 4);
	str.erase(str.begin()+2, str.end()-2);

	cout << str << endl;
}

void test_string9()
{
	string str("buider.cpp");
	size_t i = str.find('.');
	string str2 = str.substr(i);
	cout << str2 << endl;
}

void test_string10()
{
	string str("Please replace all letters of aeiou");
	size_t found = str.find_first_of("aeiou");
	while (found != string::npos)
	{
		str[found] = '*';
		found = str.find_first_of("aeiou");
	}
	cout << str << endl;
}	

int main()
{
	//test_string1();
	//test_string2();
	//test_string3();
	//test_string4();
	//test_string5();
	//test_string6();
	//test_string7();
	//test_string8();
	//test_string9();
	test_string10();

	return 0;
}